// 获取相机
function getCamera() {
    const events = {
        open() {
        }, close() {
        }, error() {
        }, success() {
        },
    };

    const webview = plus.webview.create("/static/camera/index.html", "camera-webview", {progress: {color: "#233"}});

    window.addEventListener("message", (event) => {
        const {type, data} = event.data;
        if (type === "open") {
            events.open.call(this, data);
        }
        if (type === "close") {
            events.close.call(this, data);
        }
        if (type === "error") {
            events.error.call(this, data);
        }
        if (type === "success") {
            events.success.call(this, data);
        }
    });

    function on(type, callback) {
        if (type === "open" && typeof callback === "function") {
            events.open = callback;
        }
        if (type === "close" && typeof callback === "function") {
            events.close = callback;
        }
        if (type === "error" && typeof callback === "function") {
            events.error = callback;
        }
        if (type === "success" && typeof callback === "function") {
            events.success = callback;
        }
    }

    function send(value) {
        const data = JSON.stringify(value);
        const jsCode = `window.dispatchEvent(new MessageEvent("message", { data: ${data} }))`;
        webview.evalJS(jsCode);
    }

    function open(options = {}) {
        const data = Object.assign({retryCount: 3, facingMode: "user"}, options);
        if (plus.os.name === "Android") {
            plus.android.requestPermissions(["android.permission.CAMERA"], (result) => {
                if (result.granted.length > 0) {
                    send({type: "open", data});
                    webview.show("slide-in-left", 300);
                } else {
                    plus.nativeUI.confirm("请前往设置开启摄像头权限", (e) => {
                        if (e.index === 0) openAppSetting();
                    }, {title: "提示", buttons: ["去设置", "取消"]});
                }
            });
        } else {
            send({type: "open", data});
            webview.show("slide-in-left", 300);
        }
    }

    function close(ms = 2000) {
        send({type: "close", data: ms});
    }

    return {
        on, open, close,
    };
}

// 关闭页面
function closeWebview() {
    plus.webview.currentWebview().close();
}

// 打开 app 设置
function openAppSetting() {
    const android = plus.android;
    const Intent = android.importClass("android.content.Intent");
    const Settings = android.importClass("android.provider.Settings");
    const Uri = android.importClass("android.net.Uri");
    const mainActivity = android.runtimeMainActivity();
    const intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    const uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
    intent.setData(uri);
    mainActivity.startActivity(intent);
}
